/*! \file wc_hchk.c
	\warning Open Watcom specific file.
	Implements a 'heap_valid_report' routine for Open Watcom.
    Uses OW RTL provided routine '_heapchk' to validate the
    current heap state. It is a good idea to execute this
    after a test run to nail memory corruption.
*/

#include <malloc.h>
#include <stdio.h>

#define DIM(a) (sizeof(a)/sizeof((a)[0]))

typedef struct
{
	int code;
	const char *message;
} heapchk_result;

static heapchk_result heapchk_results[] = {
	{_HEAPOK, "The heap appears to be consistent"},
	{_HEAPEMPTY, "The heap is empty"},
	{_HEAPBADBEGIN, "The heap has been damaged"},
	{_HEAPBADNODE, "The heap contains a bad node, or is damaged"}
};

static const char *get_heapchk_message(int result)
{
	int i;

	for(i = 0; i < DIM(heapchk_results); i++)
	{
		if(result == heapchk_results[i].code)
		{
			return heapchk_results[i].message;
		}
	}

	return "Udefined error";
}

void heap_valid_report(void)
{
	puts(get_heapchk_message(_heapchk()));
}
